package com.hundun.hddeal.service.lmpl;

import com.hundun.hddeal.dao.AdUserMapper;
import com.hundun.hddeal.entity.AdUser;
import com.hundun.hddeal.entity.User;
import com.hundun.hddeal.response.Result;
import com.hundun.hddeal.response.ResultUtil;
import com.hundun.hddeal.service.AdUserService;
import com.hundun.hddeal.service.TokenService;
import com.hundun.hddeal.util.MD5Util;
import com.hundun.hddeal.util.RedisUtil;
import com.hundun.hddeal.util.TextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户业务层
 * @author 谢
 */
@Service("AdUserService")
public class AdUserServiceImpl implements AdUserService {

    @Autowired
    AdUserMapper adUserMapper;
    @Autowired
    TokenService tokenService;
    @Autowired
    RedisUtil redisUtil;

    /**
     * 通过传入用户查找用户并生成token
     *
     * @param record
     * @return
     */
    @Override
    public Result selectByAdUserId(AdUser record) {
        try {
            AdUser adUser;
            if(record.getType()==1){
                if (TextUtils.isEmpty(record.getAdId())) {
                    return ResultUtil.error("请输入用户名");
                }
                if (TextUtils.isEmpty(record.getAdPwd())) {
                    return ResultUtil.error("请输入密码");
                }
                adUser = adUserMapper.selectByAdUserId(record.getAdId());
            }else {
                if(TextUtils.isEmpty(record.getAdPhone())){
                    return ResultUtil.error("手机号为空");
                }
                adUser = adUserMapper.loginForPhone(record.getAdPhone());
            }
            if (adUser == null){
                return ResultUtil.error("不存在该用户");
            }
            if (!MD5Util.getSaltverifyMD5(record.getAdPwd(),adUser.getAdPwd())){
                return ResultUtil.error("密码输入错误");
            }
            String token = tokenService.getToken(adUser.getAdId());
            adUser.setAdPwd(null);
            adUser.setPayPwd(null);
            Map<String, Object> map = new HashMap<>(2);
            map.put("adUser", adUser);
            map.put("adUserToken", token);
            return ResultUtil.success(map);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error();
        }
    }

    @Override
    public AdUser findAdUserById(String adId) {
        AdUser adUser = adUserMapper.findAdUserById(adId);
        if (adId != null) {
            return adUser;
        }
        return null;
    }

    @Override
    public Result<AdUser> register(AdUser record) {
        try {
            AdUser adUser = adUserMapper.selectByAdUserId(record.getAdId());
            if (adUser != null){
                return ResultUtil.error("已存在");
            }
            AdUser adUser1 = adUserMapper.selectPhone(record.getAdPhone());
            if(adUser1!=null){
                return ResultUtil.error("该手机号码已被注册");
            }
            String noteCode=(String)redisUtil.get(record.getAdPhone());
            if(noteCode==null ){
                return ResultUtil.error("验证码已失效");
            }else if(!record.getNoteCode().equals(noteCode)){
                return ResultUtil.error("验证码错误");
            }
            if(record.getUserCode()!=null){
                User user=adUserMapper.selectUserCode(record.getUserCode());
                if(user==null){
                    return ResultUtil.error("请填入正确的邀请码！");
                }else if(!("1".equals(user.getFz()) || "2".equals(user.getFz()))){
                    return ResultUtil.error("该邀请码不是正确的！");
                }
                record.setUserName(user.getUsername());
                record.setFz(user.getFz());
            }
            //MD5加盐加密
            String pwd=MD5Util.getSaltMD5(record.getAdPwd());
            record.setAdPwd(pwd);
            record.setPayPwd(pwd);
            adUserMapper.insertSelective(record);
            return ResultUtil.success(adUser);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error();
        }
    }

    @Override
    public Result updateMoney(String adId, Double money) {
        try {
            adUserMapper.updateMoney(adId, money);
            return ResultUtil.success();
        } catch (Exception e) {
            return ResultUtil.error();
        }
    }


    @Override
    public Result updateByPrimaryKeySelective(AdUser record) {
        try {
            adUserMapper.updateByPrimaryKeySelective(record);
            record.setPayPwd(null);
            record.setAdPwd(null);
            return ResultUtil.success(record);
        } catch (Exception e) {
            return ResultUtil.error();
        }
    }

    @Override
    public Result updateIceMoney(String adId, Double money) {
        try {
            adUserMapper.updateIceMoney(adId, money);
            return ResultUtil.success();
        } catch (Exception e) {
            return ResultUtil.error();
        }
    }

    @Override
    public Result noteLogin(String adPhone,String note) {
        try{
            if(adPhone==null){
                return ResultUtil.error("手机号为空!");
            }
            AdUser adUser;
                if(note==null){
                    return ResultUtil.error("验证码为空!");
                }
                String Rnote=(String)redisUtil.get(adPhone);
                if(Rnote==null){
                    return ResultUtil.error("验证码已过期!");
                }else if(!note.equals(Rnote)){
                    return ResultUtil.error("验证码不正确!");
                }else{
                    redisUtil.del(adPhone);
                }
            adUser = adUserMapper.loginForPhone(adPhone);
            String token = tokenService.getToken(adUser.getAdId());
            adUser.setAdPwd(null);
            adUser.setPayPwd(null);
            Map<String, Object> map = new HashMap<>(2);
            map.put("adUser", adUser);
            map.put("adUserToken", token);
            return ResultUtil.success(map);
        }catch (Exception e){
            e.printStackTrace();
            return ResultUtil.error();
        }
    }


}